<?php
class payAction extends Action
{
	/**
	 * 显示购物车主列表,
	 * 购物车每个选项的值在用户没有登陆之前用cookie
	 * cookie格式:cart["tour_idnum1"]["price"]  价格
	 * cookie格式cart["tour_idnum1"]["erprice"] 儿童价
	 * cookie格式cart["tour_idnum2"]["price"] 价格
	 * cookie格式cart["tour_idnum2"]["erprice"] 儿童价
	 */
	public function cart(){
		//获取头部
		$public=A("public");
		$tophtml=$public->top();
		
		//获取cookie里的购物车值
		
		$cart=array();
		$i=0;
		//反序列化购物车cookie
		//$syscookie=unserialize($_COOKIE["syscookie"]);
		
		foreach ($_SESSION["cart"] as $k=> $v){
			$cart[$i]=$v;
			$cart[$i]["key"]=$k;
			$i++;

		}
		
		//获取每个商品的项目属性,在模板里来展示
		foreach ($_SESSION["cart"] as $k=>$v){
			//判断是否有必选项目
			if(preg_match("/^tour$/", $k)){
				$sql="select count(*) from ".DQ."info where info2=".$vo["proid"]." and info3=560";
			}
			
			
			
		}
		
		

		
		$this->assign("syscookie",$_SESSION["cart"]);
		$this->assign("cart",$cart);
		$this->assign("tophtml",$tophtml);
		$this->display();
	}
	
	function addcart()
	{

		$prostype=$_REQUEST["prostype"];
		$proid=$_REQUEST["proid"];
		$proname=$_REQUEST["proname"];
		$chengren=$_REQUEST["chengren"];
		$et=$_REQUEST["et"];
		$cooisepriceid=$_REQUEST["cooisepriceid"];
		$maxyouhuinum=$_REQUEST["maxyouhuinum"];//最高优惠金额
		$maxjifenduihuan=$_REQUEST["maxjifenduihuan"];//最高积分兑换金额
		$public=A("public");
		//$syscookie=unserialize($_COOKIE["syscookie"]);
		
		
		//初始化获取的值
		$maxyouhuinum=($maxyouhuinum=="")?0:$maxyouhuinum;
		
		
		//获取当是线路的时候的id
       if($prostype=="tour"){
			if(!isset($_SESSION["cart"][$prostype.$proid])){
       	   		$_SESSION["cart"][$prostype.$proid]=array(
       	   			 "proid"=>$proid,
       	   		     "chengren"=>$chengren,
       	   		     "et"=>$et,
	       	   		 "cooisepriceid"=>$cooisepriceid,
	       	   		 "proname"=>$proname,
	       	   		 "propic"=>$_REQUEST["propic"],
	       	   		 "chufashijiand"=>$_REQUEST["chufashijiand"],
       	   			 "maxchengrennum"=>$_REQUEST["maxchengrennum"],//最大成人数
       	   			 "prourl"=>"/tour/view-id-".$proid,//这个商品的连接
       	   			 "maxyouhuinum"=>$maxyouhuinum,
       	   			 "maxjifenduihuan"=>$maxjifenduihuan
       	   		);
       	   		
       	   		//序列化保存
       	   		//setmycookies("syscookie",serialize($syscookie));
			}
       }
		
       alert("/pay/cart",4,"");
		
	}
	
	/**
	 * 下订单流程的第一步
	 */
	function order()
	{
		$proid=$_REQUEST["proid"];//产品id
		$priceid=$_REQUEST["cooisepriceid"];//选择的价格类型的id
		$chengren=$_REQUEST["chengren"];//成人数
		$et=$_REQUEST["et"];//儿童数
		
		//获取头部
		$public=A("public");
		$tophtml=$public->top();
		
		

		$this->assign("tophtml",$tophtml);
		$this->display();
		
		
	}
	
	/**
	 * 修改购物车中的一单项商品
	 *
	 */
	function editcart()
	{

		$protypeflag=$_REQUEST["protypeflag"];//所修改的商品类别，如tour9  表示是线路id为9的商品
		
		if(!$protypeflag){
			alert("", 1, "参数值不正确");
			die;
		}
		
		$chengren=$_REQUEST["chengren_".$protypeflag];//成人数
		$et=$_REQUEST["et_".$protypeflag];//儿童数
		$chufatime=$_REQUEST["chufatime_".$protypeflag];//具体出发日期
		$cooisepriceid=$_REQUEST["cooisepriceid_".$protypeflag];//选择价格类别的id
		$chufashijiand=$_REQUEST["chufashijiand_".$protypeflag];//时间段信息
		$roomnum=$_REQUEST["roomnum_".$protypeflag];//选择的房间数
		$agreedanfangcha=$_REQUEST["agreedanfangcha_".$protypeflag];//是否同意单房差
		$jiejifuwux=$_REQUEST["jiejifuwux_".$protypeflag];//接机服务
		$jihedidianl=$_REQUEST["jihedidianl_".$protypeflag];//集合地点
		$bixuanxiang=$_REQUEST["bixuanxiang_".$protypeflag];//必选项目
		$zixuanxiangmu=$_REQUEST["zixuanxiangmu_".$protypeflag];//自选项目
		$biaoxian=$_REQUEST["biaoxian_".$protypeflag];//保险项目
		$priceshuoming=$_REQUEST["priceshuoming_".$protypeflag];//价格的详细说明
		$chengrenprice=$_REQUEST["chengrenprice_".$protypeflag];//成人价
		$etprice=$_REQUEST["etprice_".$protypeflag];//儿童价
		$danfangcha=$_REQUEST["danfangcha_".$protypeflag];//单房价
		$proprice=$_REQUEST["proprice_".$protypeflag];//每个商品计算后的总价
		
		
		
		//更新其中一个的cookie值
		//$syscookie=unserialize($_COOKIE["syscookie"]);
		
		$_SESSION["cart"][$protypeflag]["et"]=$et;
		$_SESSION["cart"][$protypeflag]["chengren"]=$chengren;
		$_SESSION["cart"][$protypeflag]["chufatime"]=$chufatime;
		$_SESSION["cart"][$protypeflag]["cooisepriceid"]=$cooisepriceid;
		$_SESSION["cart"][$protypeflag]["chufashijiand"]=$chufashijiand;
		$_SESSION["cart"][$protypeflag]["roomnum"]=$roomnum;
		$_SESSION["cart"][$protypeflag]["agreedanfangcha"]=$agreedanfangcha;
		$_SESSION["cart"][$protypeflag]["jiejifuwux"]=$jiejifuwux;
		$_SESSION["cart"][$protypeflag]["jihedidianl"]=$jihedidianl;
		$_SESSION["cart"][$protypeflag]["bixuanxiang"]=$bixuanxiang;
		$_SESSION["cart"][$protypeflag]["zixuanxiangmu"]=$zixuanxiangmu;
		$_SESSION["cart"][$protypeflag]["biaoxian"]=$biaoxian;
		$_SESSION["cart"][$protypeflag]["priceshuoming"]=$priceshuoming;
		$_SESSION["cart"][$protypeflag]["chengrenprice"]=$chengrenprice;
		$_SESSION["cart"][$protypeflag]["etprice"]=$etprice;
		$_SESSION["cart"][$protypeflag]["danfangcha"]=$danfangcha;
		$_SESSION["cart"][$protypeflag]["proprice"]=$proprice;

		//setmycookies("syscookie",serialize($syscookie));

		alert("/pay/cart",4,"");
	}
	
	/**
	 * 删除购物车里的其中一个商品
	 
	 */
	function delcart(){
		
		$protypeflag=$_REQUEST["protypeflag"];
		if($protypeflag){
			
			unset($_SESSION["cart"][$protypeflag]);
			
			alert("/pay/cart",2,"删除成功");
		}
		else {
			alert("/pay/cart",2,"失败");
		}
		
	}
	
	/**
	 * 进入支付流程,填写写顾客的信息
	 * 
	 */
	function paytwo()
	{
		//获取头部
		$public=A("public");
		$tophtml=$public->top();
		//$syscookie=unserialize($_COOKIE["syscookie"]);
		
		
		$this->assign("syscookie",$_SESSION);
		$this->assign("tophtml",$tophtml);
		$this->display();
	}
	
		/**
	 * 进入获取客人信息，生成订单
	 * 
	 */
	function paythree()
	{
		
		include RootDir.'/inc/MailClass.php';
		
		//获取头部
		$public=A("public");
		$tophtml=$public->top();
		//$syscookie=unserialize($_COOKIE["syscookie"]);
		
		
		//==========================生成订单号
		if(count($_SESSION["cart"])<1){
			alert("/",2,"购物车里没有线路或酒店，请先去选购");
			die();
		}
		
		$showinfo=array();
		$orderform=A("orderform");//初始化订单表的模型
		
		$ordernolist="";//订单号列表，多个订单号时用逗号分隔
		$dingjiallnumlist="";//各个订单的订金数,多个时用逗号分隔
		$dingjinallnum=0;//各个订单的订金之和
		
		
		
		
		
		foreach ($_SESSION["cart"] as $k=>$v){
			$userid=($_COOKIE["uchome_uid"]!="")?$_COOKIE["uchome_uid"]:0;
			$totalmoney=0;
			
			//获取这条价格的信息
			if(!is_numeric($v["cooisepriceid"])) die("你没有选择价格");
			$pricers=$public->query("select * from ".DQ."price where price0=".$v["cooisepriceid"]);
			
			
			$dingjingzifubl=($pricers[0]["price4"]=="" or (int)$pricers[0]["price4"] >100)?100:$pricers[0]["price4"];//定金支付比例
			//定金金额
			$dingjingnum=$v["proprice"]*$dingjingzifubl/100;
			
			//定金支付期限
			if(!is_numeric($pricers[0]["price18"])){
				$djzfqx=time()+3*3600*24;//默认三天内支付
			}else{
				$djzfqx=time()+$pricers[0]["price18"]*3600*24;
			}
			
			//余款支付期限
		if(!is_numeric($pricers[0]["price14"])){
				$yukuzfqx=time()+7*3600*24;//默认七天内支付
			}else{
				$yukuzfqx=time()+$pricers[0]["price14"]*3600*24;
			}
			
			
			
			//组合订单内容	
			$ordercontent="<b>类型(出发时间段):</b>".$v["chufashijiand"]."<br>";
			$ordercontent.="<b>人数</b>:成人:".$v["chengren"]."位,儿童:".$v["et"]."个<br>";
			
			
			$ordercontent.="<b>选择的项目</b>:<br>";
			if($v["jiejifuwux"]!="") $ordercontent.=" 接机服务:".preg_replace("/\^[\d\.]+$/", "", $v["jiejifuwux"])."<br>";
			if($v["bixuanxiang"]!="") $ordercontent.=" 必选项目:".preg_replace("/\^[\d\.]+$/", "", $v["bixuanxiang"])."<br>";
			if($v["roomnum"]!="") $ordercontent.="选择的房间数:".$v["roomnum"]."间<br>";
			$ordercontent.="是否同意拼房:".($v["agreedanfangcha"]=="1")?"是":"否";
			
			
			if($v["zixuanxiangmu"]){
				$ordercontent.="自选项目:";
				foreach ($v["zixuanxiangmu"] as $vz){
					$ordercontent.=preg_replace("/\^[\d\.]+$/", "", $vz)." ";
				}
				$ordercontent.="<br>";
			}
			
			if($v["biaoxian"]){
				$ordercontent.="购买的保险项目:";
				foreach ($v["biaoxian"] as $vz){
					$ordercontent.=preg_replace("/\^[\d\.]+$/", "", $vz)." ";
				}
				$ordercontent.="<br>";
			}
			//游客信息
			$totalren=(int)$v["chengren"]+(int)$v["et"];
			for($i=1;$i<=$totalren;$i++){
				$ordercontent.="第$i位客人:".$_REQUEST["gukename_$k_$i"].",".$_REQUEST["tel_$k_$i"].",".$_REQUEST["sex_$k_$i"]."<br>";
			}
			if($v["priceshuoming"]!="") $ordercontent.="价格计算公式:".$v["priceshuoming"]."<br>";
			
				$orderno=$this->creatorderno($k);

				$data=array();
				$data["orderform1"]=$v["proid"];
				$data["orderform2"]=554;
				$data["orderform3"]=$orderno;
				$data["orderform4"]=$v["proname"];
				$data["orderform6"]=time();
				$data["orderform7"]=$userid;
				$data["orderform5"]=$_REQUEST["usernamelx"];
				$data["orderform26"]=$_REQUEST["useremail"];
				$data["orderform9"]=strtotime($v["chufatime"]);
				$data["orderform16"]=$v["proprice"];
				$data["orderform17"]=$dingjingnum;
				$data["orderform10"]=$ordercontent;
				$data["orderform34"]=$v["cooisepriceid"];
				$data["orderform18"]=$djzfqx;
				$data["orderform19"]=$v["proprice"]-$dingjingnum;//余款金额
				$data["orderform20"]=$yukuzfqx;//余款支付期限
				
				
			if($orderform->createorder($data))
			{
				
				//当插入数据库成功后，整理数据，显示页面
				$showinfo[$k]["order"]=$orderno;//订单号
				$showinfo[$k]["usernamelx"]=$_REQUEST["usernamelx"];//联系人
				$showinfo[$k]["useremail"]=$_REQUEST["useremail"];//联系人邮件
				$showinfo[$k]["totalmoney"]=$v["proprice"];//每个商品的总价
				$showinfo[$k]["dingjingmoney"]=$v["proprice"]*$dingjingzifubl/100;//定金金额
				
				
				$showinfo[$k]["djzfqx"]=$djzfqx;//定金支付期限
				$showinfo[$k]["proname"]=$v["proname"];//产品名称
				$showinfo[$k]["chufatime"]=$v["chufatime"];//出发时间
				$showinfo[$k]["dingjingnum"]=$dingjingnum;//订金金额
				$showinfo[$k]["proid"]=$v["proid"];//产品id号
				$showinfo[$k]["createtime"]=$data["orderform6"];//订单生成的时间
				
				$dingjiallnumlist.=$showinfo[$k]["dingjingmoney"].",";
				$dingjinallnum+=$showinfo[$k]["dingjingmoney"];
				$ordernolist.=$orderno.",";
			}
		}
		
		
		//发送邮件
		$mailrsc=A("mail");
		$info=array();
		$info["username"]=$_REQUEST["usernamelx"];
		$bodyhtml=$mailrsc->createorder($info,$showinfo);
		if($bodyhtml!="") sendmygmail("梦之旅",$_REQUEST["useremail"],"您的订单已经生成成功",$bodyhtml."");
		

		//生成支付选项
		$ordernolist=substr($ordernolist, 0,-1);
		$dingjiallnumlist=substr($dingjiallnumlist, 0,-1);
		
		
		
		$paytype=A("paytype");
		
		//支付宝
		$arr["out_trade_no"]=$ordernolist;//订单号
		$arr["aliorder"]="梦之旅旅游产品订单";
		$arr["alibody"]="旅游产品定金";
		$arr["alimoney"]=$dingjinallnum;//支付金额
		$arr["productid"]=$tid;//产品的id号
		$arr["paymoneytype"]="j1";//支付的是定金还是余款  dj表示支付的是定金
		$arr["alimoneylist"]=$dingjiallnumlist;//各个订单的定金列表
		$alipay_pay=$paytype->alipay($arr);
		
		
		//环讯信用卡在线支付人民币
		$arrco["pBillNo"]=$ordernolist;
		$arrco["pAmount"]=$dingjinallnum; 
		$arrco["pAttachpaty"]="j1";
		$arrco["productid"]=$tid;//产品的id号
		$arrco["pGoodsInfo"]="梦之旅旅游产品服务";
		$arrco["pAttach"]=$dingjiallnumlist;//各个订单的定金列表
		$cardonlinepayrmb=$paytype->CardOnlineRmb($arrco);
		
		
		
		
		
		$this->assign("showinfo",$showinfo);
		$this->assign("syscookie",$_SESSION);
		$this->assign("alipay_pay",$alipay_pay);
		$this->assign("alipay_pay",$alipay_pay);
		$this->assign("cardonlinepayrmb",$cardonlinepayrmb);
		$this->display();
	}
	
	/**
	 * 生成订单号,生成的格式如:t0000001 或h0000001
	 * 条件是在最大的订单号值是加1
	 * 
	 */
	function creatorderno($type)
	{
		$public=A("public");
		$rs=$public->query("select orderform3 from ".DQ."orderform order by orderform0 desc limit 0,1");

		if(preg_match("/^tour[0-9]+$/", $type)) $ttype="t";
		if(preg_match("/^hotel[0-9]+$/", $type)) $ttype="h";
		
		if($rs){
			$temp=$rs[0]["orderform3"];
			$temp=preg_replace("/^[a-zA-Z]+/", "", $temp);
			$temp+=1;
			$n=strlen($temp);
			return $ttype."".preg_replace("/0{".$n."}$/", $temp, "0000000");
		}
		else
		{
			return $ttype."".preg_replace("/0{".$n."}$/", $temp, "0000001");
		}
	}
	
	/**
	 * 当客人支付成功时显示的页面
	 */
	function showpaysuss(){
		$orderlist=$_REQUEST["orderlist"];//订单号列表 格式应为:
	}
	
	
}

?>